﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.OAWeb.Base;
using System.IO;
using Basic4Net.Utility.IO;
using System.Collections;
using System.Text;
using System.ComponentModel;

namespace Basic4Net.OAWeb.CustomControl
{
	public partial class UpDownLoadControl : BaseUserControl
	{
		/// <summary>
		/// 文件存放位置
		/// </summary>
		[Browsable(true),
		Description("文件存放位置，例如Temp为更目录下的Temp文件夹。"),
		Category("Data"),
		DefaultValue("Temp")]
		public string SavePath
		{
			get
			{
				object obj = ViewState["SavePath"];
				return (obj == null) ? "Temp" : (string)obj;
			}
			set
			{
				ViewState["SavePath"] = value;
			}
		}

		/// <summary>
		/// 文件存放位置
		/// </summary>
		[Browsable(true),
		Description("最大附件数"),
		Category("Data"),
		DefaultValue("5")]
		public int MaxNumber
		{
			get
			{
				object obj = ViewState["MaxNumber"];
				return (obj == null) ? 5 : (int)obj;
			}
			set
			{
				ViewState["MaxNumber"] = value;
			}
		}

		/// <summary>
		/// 设置存在文件的ID和文件名
		/// </summary>
		public Dictionary<int, string> OldFiles = new Dictionary<int, string>();

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				AddOldFiles();
			}
		}

		private void AddOldFiles()
		{
			StringBuilder sb = new StringBuilder();
			int i = 0;
			foreach (KeyValuePair<int, string> kvp in OldFiles)
			{
				sb.Append(String.Format(@"<span id=""{3}_serverspan{0}"" style="""">{2}</span>
<input id=""{3}_serverHidden{0}"" type=""hidden"" value=""{1}""/>
<img id=""{3}_serverdelete{0}"" alt=""删除"" src=""{4}"" onclick=""{3}DeleteLine(this, '{3}_serverspan{0}','{3}_serverHidden{0}');"" />
<br id=""{3}_serverbr{0}"" />", i, kvp.Key, kvp.Value, this.ClientID, Page.ResolveClientUrl("~/App_Themes/ui-lightness/ProjectImages/del.gif")));
				++i;
			}
			this.theFilesShow.InnerHtml = sb.ToString();
		}

		/// <summary>
		/// 获取新上传文件的文件名和byte数组的集合
		/// </summary>
		/// <returns></returns>
		public Dictionary<string, byte[]> GetNewFileByte()
		{
			Dictionary<string, byte[]> FilesInfo = new Dictionary<string, byte[]>();
			HttpFileCollection Files = Request.Files;
			for (int Items = 0; Items < Files.Count; Items++)
			{
				HttpPostedFile PostedFile = Files[Items];
				if (PostedFile.FileName != String.Empty)
				{
					byte[] Filebyte = new byte[PostedFile.ContentLength];
					PostedFile.InputStream.Read(Filebyte, 0, PostedFile.ContentLength);
					FilesInfo.Add(FileTools.GetFileName(PostedFile.FileName), Filebyte);
				}
			}
			return FilesInfo;
		}

		/// <summary>
		/// 获取新上传文件的文件名和文件流的集合
		/// </summary>
		/// <returns></returns>
		public Dictionary<string, Stream> GetNewStream()
		{
			Dictionary<string, Stream> FilesInfo = new Dictionary<string, Stream>();
			HttpFileCollection Files = Request.Files;
			for (int Items = 0; Items < Files.Count; Items++)
			{
				HttpPostedFile PostedFile = Files[Items];
				if (PostedFile.FileName != String.Empty)
					FilesInfo.Add(FileTools.GetFileName(PostedFile.FileName), PostedFile.InputStream);
			}
			return FilesInfo;
		}

		/// <summary>
		/// 获取上传文件的文件名和存储后的GUID名
		/// </summary>
		/// <returns>key为GUID，value为文件名</returns>
		public Dictionary<string, string> GetNewFileName()
		{
			Dictionary<string, string> FilesInfo = new Dictionary<string, string>();
			HttpFileCollection Files = Request.Files;
			for (int Items = 0; Items < Files.Count; Items++)
			{
				HttpPostedFile PostedFile = Files[Items];
				if (PostedFile.FileName != String.Empty)
				{
					string tempguid = Guid.NewGuid().ToString();
					byte[] Filebyte = new byte[PostedFile.ContentLength];
					PostedFile.InputStream.Read(Filebyte, 0, PostedFile.ContentLength);
					FileTools.CreateTempFile(Server.MapPath("~/" + this.SavePath), Filebyte, tempguid);
					FilesInfo.Add(tempguid, FileTools.GetFileName(PostedFile.FileName));
				}
			}
			return FilesInfo;
		}

		/// <summary>
		/// 获取被删除的文件的GUID
		/// </summary>
		/// <returns></returns>
		public List<int> GetDeleteID()
		{
			string temp = this.HideDeleteIDs.Value.Trim(',');
			string[] strIDs = temp.Split(',');
			List<int> IDs = new List<int>();
			for (int i = 0; i < strIDs.Length; i++)
				if (strIDs[i] != String.Empty)
					IDs.Add(int.Parse(strIDs[i]));
			return IDs;
		}
	}
}
